var prefix = jsctx + "/orderInfo";

var saasCountOrderInfoPage = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        getEchars()
    };

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        getEchars()
    };

    function exportCountOrderInfo() {
        $.fileDownload(prefix + "/exportSaasCountOrderInfo",{ // 后台请求链接
            httpMethod : "get",　　　　　　　　// 请求方式
            contentType:'application/json',　 // 请求头
            data : {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                startTime: $('#startTime').val(),
                endTime: $("#endTime").val(),
                hlhtOperatorId: $("#hlhtOperatorId").val(),
                equipmentHlhtOperatorId: $("#equipmentHlhtOperatorId").val(),
                stationName:$("#stationName").val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...",{time:-1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！",{time:1500});
            },
            failCallback: function (data) {
                var str = "{"+data.substring(data.indexOf("{")+1,data.indexOf("}"))+"}";
                layer.msg(JSON.parse(str).message);
            }
        });

    }

    //获取数据
    function getEchars() {
        var myChart = echarts.init(document.getElementById('main'));
        myChart.showLoading();
        $.ajax({
            "url": prefix + "/saasCountOrderInfo",
            "method": "GET",
            "data": {
                limit: 50,
                offset: 0,
                startTime: $('#startTime').val(),
                endTime: $("#endTime").val(),
                hlhtOperatorId: $("#hlhtOperatorId").val(),
                equipmentHlhtOperatorId: $("#equipmentHlhtOperatorId").val(),
                stationName:$("#stationName").val()

            },
            "success": function (data) {
                myChart.hideLoading();
                d(data, myChart);
                $("#countData").html("<strong>充电量:</strong>" + data.totalPower + "度&nbsp;&nbsp;&nbsp;&nbsp;<strong>电费:</strong>" + data.totalElecMoney +
                    "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>服务费:</strong>" + data.totalSeviceMoney + "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>总金额:</strong>" + data.totalMoney + "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>第三方优惠:</strong>" + data.discountAmount +
                    "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>第三方实付:</strong>" + data.payAmount + "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>saas优惠:</strong>" + data.saasDiscountAmount + "元&nbsp;&nbsp;&nbsp;&nbsp;<strong>saas实付:</strong>" + data.saasPayAmount+"元")
            }
        })
    }

    function d(data, myChart) {
        // 基于准备好的dom，初始化echarts实例
        option = {
            color: ['#3398DB'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                left: '0%',
                right: '0%',
                bottom: '0%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['充电量', '电费', '服务费', '总金额','第三方优惠','第三方实付','saas优惠','saas实付'],
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis: [
                {
                    name: '数量',
                    type: 'value'
                }
            ],
            series: [
                {
                    type: 'bar',
                    barWidth: '60%',
                    data: [data.totalPower, data.totalElecMoney, data.totalSeviceMoney, data.totalMoney, data.discountAmount, data.payAmount, data.saasDiscountAmount,data.saasPayAmount]
                }
            ]
        };
        myChart.setOption(option);

    };

    return {
        init: init,
        reLoad: reLoad,
        exportCountOrderInfo:exportCountOrderInfo
    };
})();

